package com.eduhzy.ttw.work.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.work.R;
import com.eduhzy.ttw.work.R2;
import com.eduhzy.ttw.work.di.component.DaggerWorkHomeComponent;
import com.eduhzy.ttw.work.di.module.WorkHomeModule;
import com.eduhzy.ttw.work.mvp.contract.WorkHomeContract;
import com.eduhzy.ttw.work.mvp.model.entity.WorkListData;
import com.eduhzy.ttw.work.mvp.presenter.WorkHomePresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import org.simple.eventbus.Subscriber;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-1 15:42:29
 */
@Route(path = RouterHub.WORK_WORKHOMEFRAGMENT)
public class WorkHomeFragment extends BaseFragment<WorkHomePresenter> implements WorkHomeContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    BaseQuickAdapter<WorkListData, AutoBaseViewHolder> mAdapter;
    @Inject
    List<WorkListData> mList;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    private int type;

    public static WorkHomeFragment newInstance(int type) {
        WorkHomeFragment fragment = new WorkHomeFragment();
        fragment.setData(type);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerWorkHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .workHomeModule(new WorkHomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_work_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(mLayoutManager);
        mSwipeLayout.setOnRefreshListener(() -> getData(true));
        mAdapter.setOnLoadMoreListener(() -> getData(false), mRvList);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            WorkListData workListData = mAdapter.getData().get(position);
            ARouter.getInstance().build(RouterHub.WORK_WORKDETAILACTIVITY)
                    .withString(Intent.EXTRA_TITLE, Constants.WORK_DETAIL)
                    .withInt(Constants.ROUTER_INTEGER, workListData.getId())
//                    .withBoolean(Constants.ROUTER_BOOLEAN, isHistory)
                    .navigation(getActivity());
        });
        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            WorkListData workListData = mAdapter.getData().get(position);
            RxUtil.showDeleteDialog(getActivity(), "删除作业", "确定要删除吗", (dialog, index) -> {
                if (index == 0) {
                    dialog.dismiss();
                } else {
                    mPresenter.hasWorkSubmit(workListData.getId(), position);
                    dialog.dismiss();
                }
            });
            return true;
        });
        getData(true);
    }

    private void getData(boolean pullToRefresh) {
        mPresenter.list4Teacher(pullToRefresh, type);
    }

    @Override
    public void setData(@Nullable Object data) {
        this.type = (int) data;
    }

    @Override
    public void showLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Subscriber(tag = RouterHub.WORK_WORKHOMEFRAGMENT)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.WORK_PUBLISH_SUCCESS:
                if (type == 1) {
                    getData(true);
                }
                break;
            default:
        }
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void setEmptyView() {
        if (ObjectUtils.isNotEmpty(mAdapter) && mAdapter.getEmptyViewCount() <= 0) {
            View view = View.inflate(getActivity(), R.layout.public_custom_empty_layout, null);
            ImageView iv = view.findViewById(R.id.iv_logo);
            iv.setImageResource(R.drawable.no_work);
            QMUIRoundButton btn = view.findViewById(R.id.btn_submit);
            btn.setText("发布作业");
            btn.setOnClickListener((view1) -> {
                ARouter.getInstance().build(RouterHub.WORK_PUBLISHWORKACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.PUBLISH_WORK)
                        .navigation(getActivity());
            });
            mAdapter.setEmptyView(view);
        }
    }
}
